<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/12/16
 * Time: 8:26
 */



include_once '../tools/common.php';
include_once '../tools/DbTools.php';
$is_login = getLoginStatus();
if(!$is_login){
    echo '尚未登入，<a href="loginUi.php">请登入</a>';
    die;
}else{
    if(empty($_POST['cardCheck'])){
        echo '没有选择任何卡<a href="otherCards.php">重新选择</a>';die;
    }
    session_start();
    $usreInfo = $_SESSION[$_COOKIE['Token']];
    $mainCardID =  $usreInfo['CardId'];
    //遍历所有选中的卡
    DbTools::InitDb();
    $has_err = false;
    for($i=0;$i<count($_POST['cardCheck']);$i++){
        //处理一张卡
        $TempCardId = $_POST['cardCheck'][$i];
        $sql = "select CardMoney from bankcard where CardId = ".$TempCardId;
        $result = DbTools::select($sql);
        $money = $result[0]['CardMoney'];
        transferMoney($TempCardId,$mainCardID,$money,1);
        //状态更改
        $sql = "update bankcard set CardState=-1 where CardId = ".$TempCardId;
        $result = DbTools::noSelect($sql);
        if(!$result['status']){
            echo '状态更改: '.$result['message'];
            $has_err = true;
            break;
        }
    }
    //关闭连接
    DbTools::close();
    if(!$has_err){
        echo '销卡成功<a href="otherCards.php">继续销卡</a>&nbsp;&nbsp;&nbsp;<a href="../user/userInfo.php">个人中心</a>';
    }
}
?>